package com.heatingcharge.service.unit.impl;

import com.google.common.collect.Lists;
import com.heatingcharge.dto.ChargeInfoDTO;
import com.heatingcharge.dto.ResUnitDTO;
import com.heatingcharge.entity.unit.ResUnit;
import com.heatingcharge.mapper.unit.ResUnitMapper;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.service.charge.IChargeInfoService;
import com.heatingcharge.service.unit.IResHouseService;
import com.heatingcharge.service.unit.IResUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heatingcharge.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 单元表 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-07-12
 */
@Service
public class ResUnitServiceImpl extends ServiceImpl<ResUnitMapper, ResUnit> implements IResUnitService {

    @Autowired
    IResHouseService resHouseService;

    @Autowired
    IChargeInfoService chargeInfoService;

    @Override
    public List<ResUnit> selectResUnit(ResUnitDTO resUnitDto) {
        return baseMapper.selectResUnit(resUnitDto);
    }

    @Override
    public List<TreeVo> selectResUnitTree(ResUnitDTO resUnitDto) {
        List<ResUnit> resUnits = baseMapper.selectResUnit(resUnitDto);
        List<TreeVo> treeVos= Lists.newArrayList();
        TreeVo treeVo;
        SearchParam searchParam;
        if (resUnits.size()>0){
            for (int i = 0; i < resUnits.size(); i++) {
                searchParam=SearchParam.builder()
                        .unitId(resUnits.get(i).getId())
                        .build();
                treeVo=TreeVo.builder()
                        .value(resUnits.get(i).getId())
                        .id("unitId="+resUnits.get(i).getId())
                        .label(resUnits.get(i).getNum())
                        .build();
                if(resHouseService.getHouses(searchParam).size()>0) {
                    List<ChargeInfoDTO> houseChargeInfo = chargeInfoService.getHouseChargeInfo(searchParam);
                    treeVo.setChargeInfos(houseChargeInfo);
                }
                treeVos.add(treeVo);
            }
        }
        return treeVos;
    }
}
